class EntriesController < ApplicationController

    before_filter :login_required, :except => [:index, :show]
    before_filter :authorization_required, :only => [:destroy, :edit, :update]
    before_filter :get_aggregation
    
    layout "entry"

    # GET /entries
    # GET /entries.xml
    def index
        @entries = Entry.find(:all)

        respond_to do |format|
            format.html # index.html.erb
            format.xml  { render :xml => @entries }
        end
    end

    # GET /entries/1
    # GET /entries/1.xml
    def show
        @entry = Entry.get_entry(params[:id])
        
        # record that it was viewed
        if logged_in?
            current_user.entries << @entry rescue 0
        end
        
        respond_to do |format|
            format.html {  }
            format.xml  { render :xml => @entry }
        end
    end

    # adds the entry to the list of marked entries for a given user
    def mark_entry
        @entry = Entry.get_entry(params[:id])
        current_user.marked << @entry
        @entry_state = 'Entry Marked'
        
        respond_to do |format|
            format.html { render(:layout => false) }
            format.xml  { head :ok }
        end
    end
    
    # GET /entries/new
    # GET /entries/new.xml
    def new
        @entry = Entry.new

        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @entry }
        end
    end

    # GET /entries/1/edit
    def edit
        @entry = Entry.find(params[:id])
    end

    # POST /entries
    # POST /entries.xml
    def create
        @entry = Entry.new(params[:entry])

        respond_to do |format|
            if @entry.save
                flash[:notice] = 'Entry was successfully created.'
                format.html { redirect_to(@entry) }
                format.xml  { render :xml => @entry, :status => :created, :location => @entry }
            else
                format.html { render :action => "new" }
                format.xml  { render :xml => @entry.errors }
            end
        end
    end

    # PUT /entries/1
    # PUT /entries/1.xml
    def update
        @entry = Entry.find(params[:id])

        respond_to do |format|
            if @entry.update_attributes(params[:entry])
                flash[:notice] = 'Entry was successfully updated.'
                format.html { redirect_to(@entry) }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @entry.errors }
            end
        end
    end

    # DELETE /entries/1
    # DELETE /entries/1.xml
    def destroy
        @entry = Entry.find(params[:id])
        @entry.destroy

        respond_to do |format|
            format.html { redirect_to(entries_url) }
            format.xml  { head :ok }
        end
    end
    
    protected
    def get_aggregation
        if params[:aggregation_id] != '0'
            @aggregation = params[:aggregation_id] ? Aggregation.find(params[:aggregation_id]) : nil
        else
            @aggregation = Aggregation.new
        end
    end
    
    def authorization_required

    end
end
